package com.chima.dao;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.chima.domain.Job;



public class JobDao extends HibernateDaoSupport{
 
	//分页查询
	public List<Job> findByCriteria(DetachedCriteria criteria, int firstResult,int maxResults) {
		return this.getHibernateTemplate().findByCriteria(criteria,firstResult,maxResults);
	}
   //查找总页数
	public int findByTotalCount( String date, int emp, int jobstyle)  {
		long temp=2312312l;
		if(emp==0){
			if(date==null){
				 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 
		         Calendar   calendar=Calendar.getInstance(); 
		         calendar.setTime(new Date());
		         date= sdf.format(calendar.getTime());
				List list= this.getHibernateTemplate().find("select count(*) from Job where YEAR(creatDate)=YEAR('"+date+"') and jobstyle="+jobstyle+"");
				 temp = (Long) list.get(0);
			}else if(date!=null){
				 int day=Integer.parseInt( date.substring(5,7));
				 int year=Integer.parseInt( date.substring(0,4));
				 String  First =getFirstDayOfMonth(year,day);
				 String  last =getLastDayOfMonth(year,day);
				List list= this.getHibernateTemplate().find("select count(*) from Job where creatDate >= '"+First+"' and creatDate <= '"+last+"' and jobstyle="+jobstyle+" ");
				 temp = (Long) list.get(0);
			}
		   
		}else if(emp!=0){
			if(date==null){
				 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 
		         Calendar   calendar=Calendar.getInstance(); 
		         calendar.setTime(new Date());
		         date= sdf.format(calendar.getTime());
				List list= this.getHibernateTemplate().find("select count(*) from Job where YEAR(creatDate)=YEAR('"+date+"') and employee.id="+emp+" and jobstyle="+jobstyle+" ");
				 temp = (Long) list.get(0);
			}else if(date!=null){
				 int day=Integer.parseInt( date.substring(5,7));
				 int year=Integer.parseInt( date.substring(0,4));
				 String  First =getFirstDayOfMonth(year,day);
				 String  last =getLastDayOfMonth(year,day);
				List list= this.getHibernateTemplate().find("select count(*) from Job where creatDate >= '"+First+"' and creatDate <= '"+last+"' and employee.id="+emp+" and jobstyle="+jobstyle+" ");
				 temp = (Long) list.get(0);
			}
		}
		
		return (int) temp;
	}
	public int findByTotalCount1( String date, int emp, int jobstyle)  {
		long temp=2312312l;
		if(emp==0){
			if(date==null){
				 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 
		         Calendar   calendar=Calendar.getInstance(); 
		         calendar.setTime(new Date());
		         date= sdf.format(calendar.getTime());
				List list= this.getHibernateTemplate().find("select count(*) from Job where YEAR(creatDate)=YEAR('"+date+"') and jobstyle="+jobstyle+"");
				 temp = (Long) list.get(0);
			}else if(date!=null){
				 int day=Integer.parseInt( date.substring(5,7));
				 int year=Integer.parseInt( date.substring(0,4));
				 String  First =getFirstDayOfMonth(year,day);
				 String  last =getLastDayOfMonth(year,day);
				List list= this.getHibernateTemplate().find("select count(*) from Job where creatDate >= '"+First+"' and creatDate <= '"+last+"' and jobstyle="+jobstyle+" ");
				 temp = (Long) list.get(0);
			}
		   
		}else if(emp!=0){
			if(date==null){
				 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 
		         Calendar   calendar=Calendar.getInstance(); 
		         calendar.setTime(new Date());
		         date= sdf.format(calendar.getTime());
				List list= this.getHibernateTemplate().find("select count(*) from Job where YEAR(creatDate)=YEAR('"+date+"') and employee.id="+emp+" and jobstyle="+jobstyle+" ");
				 temp = (Long) list.get(0);
			}else if(date!=null){
				 int day=Integer.parseInt( date.substring(5,7));
				 int year=Integer.parseInt( date.substring(0,4));
				 String  First =getFirstDayOfMonth(year,day);
				 String  last =getLastDayOfMonth(year,day);
				List list= this.getHibernateTemplate().find("select count(*) from Job where creatDate >= '"+First+"' and creatDate <= '"+last+"' and employee.id="+emp+" and jobstyle="+jobstyle+" ");
				 temp = (Long) list.get(0);
			}
		}
		
		return (int) temp;
	}
	public void save( Job  job) {
		this.getHibernateTemplate().save( job);
	}
	public void update(Job job) {
		this.getHibernateTemplate().update( job);
	}
	
	public void delete( Job job) {
		this.getHibernateTemplate().delete( job);
		
	}
	public Job findById( int id) {
		Session session=this.getSession();
		Job  job = (Job)session.createQuery("from Job j where j.id = ?").setParameter(0, id).uniqueResult();
		super.releaseSession(session);
		return job;
		
	}
	public Job  findByName( String name) {
		Session session=this.getSession();
		Job  job = (Job)session.createQuery("from Job j where j.name = ?").setParameter(0, name).uniqueResult();
		super.releaseSession(session);
		return job;
	}
	public Job  findByNameAndDateAnddept( String name,Date date,int emp) {
		Session session=this.getSession();
		Job  job = (Job)session.createQuery("from Job j where j.name = ? and j.creatDate=? and j.employee.id=?").setParameter(0, name).setParameter(1, date).setParameter(2, emp).uniqueResult();
		super.releaseSession(session);
		return job;
	}
	
	 /***
	  * 得到某年某月的第一天
	  * @param year
	  * @param month
	 * @throws ParseException 
	 */
	public String getFirstDayOfMonth(int year, int month)   {
		 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 
		  Calendar cal = Calendar.getInstance();
		  cal.set(Calendar.YEAR, year);
		  cal.set(Calendar.MONTH, month-1);
		  cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DATE));
		  String first=new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()); 
			return first;
		
	}
  /**
	  * 得到某年某月的最后一天
	  *
	  * @param year
	  * @param month
	*/
	 public  String getLastDayOfMonth(int year, int month) {
		 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 
		  Calendar cal = Calendar.getInstance();
		  cal.set(Calendar.YEAR, year);
		  cal.set(Calendar.MONTH, month-1);
		  cal.set(Calendar.DAY_OF_MONTH, 1);
		  int value = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		  cal.set(Calendar.DAY_OF_MONTH, value);
		 String last=new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()); 
		
		  return last ;
	 }
}
